//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2005-2007
//
// COM object model: the cursor object.
//

# include "StdAfx.h"
# include "COMCursor.h"
# include "Cursors.h"

STDMETHODIMP CComCursor::Delete () {
	DeleteCursor (m_cursor);
	return S_OK;
}

STDMETHODIMP CComCursor::get_Latitude  (double * _ret) {
	* _ret = m_cursor.pt.y;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Longitude (double * _ret) {
	* _ret = m_cursor.pt.x;
	return S_OK;
}

STDMETHODIMP CComCursor::SetPosition (double _dfLatitude, double _dfLongitude) {
	m_cursor.pt.x = _dfLongitude;
	m_cursor.pt.y = _dfLatitude;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Heading (double * _ret) {
	* _ret = m_cursor.fHeading;
	return S_OK;
}

STDMETHODIMP CComCursor::put_Heading (double _dfVal) {
	m_cursor.fHeading = _dfVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_HasHeading (VARIANT_BOOL * _ret) {
	* _ret = m_cursor.bHasHeading ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CComCursor::put_HasHeading (VARIANT_BOOL _Val) {
	m_cursor.bHasHeading = _Val != VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Color (long * _ret) {
	* _ret = m_cursor.col;
	return S_OK;
}

STDMETHODIMP CComCursor::put_Color (long _lVal) {
	m_cursor.col = _lVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_ContourColor (long * _ret) {
	* _ret = m_cursor.colContour;
	return S_OK;
}

STDMETHODIMP CComCursor::put_ContourColor (long _lVal) {
	m_cursor.colContour = _lVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Radius (long * _ret) {
	* _ret = m_cursor.nR;
	return S_OK;
}

STDMETHODIMP CComCursor::put_Radius (long _lVal) {
	m_cursor.nR = _lVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_ArrowColor (long * _ret) {
	* _ret = m_cursor.colArrow;
	return S_OK;
}

STDMETHODIMP CComCursor::put_ArrowColor (long _lVal) {
	m_cursor.colArrow = _lVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Label (BSTR * _ret) {
	USES_CONVERSION;
	* _ret = ::SysAllocStringLen (A2OLE (m_cursor.strLabel.c_str ()), m_cursor.strLabel.size ());
	return S_OK;
}

STDMETHODIMP CComCursor::put_Label (BSTR _bstrVal) {
	USES_CONVERSION;
	m_cursor.strLabel = OLE2A (_bstrVal);
	return S_OK;
}

STDMETHODIMP CComCursor::get_LabelColor (long * _ret) {
	* _ret = m_cursor.colLabel;
	return S_OK;
}

STDMETHODIMP CComCursor::put_LabelColor (long _lVal) {
	m_cursor.colLabel = _lVal;
	return S_OK;
}

STDMETHODIMP CComCursor::get_LabelX0 (long * _ret) {
	* _ret = m_cursor.xLabel0;
	return S_OK;
}

STDMETHODIMP CComCursor::get_LabelY0 (long * _ret) {
	* _ret = m_cursor.yLabel0;
	return S_OK;
}

STDMETHODIMP CComCursor::get_LabelAlignment (long * _ret) {
	* _ret = m_cursor.uiAlignMask;
	return S_OK;
}

STDMETHODIMP CComCursor::SetLabelAlignment (long _x0, long _y0, long _lAlignMask) {
	m_cursor.xLabel0 = _x0;
	m_cursor.yLabel0 = _y0;
	m_cursor.uiAlignMask = _lAlignMask;
	return S_OK;
}

STDMETHODIMP CComCursor::get_Hidden (VARIANT_BOOL * _ret) {
	* _ret = m_cursor.bHidden ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CComCursor::put_Hidden (VARIANT_BOOL _Val) {
	m_cursor.bHidden = _Val != VARIANT_FALSE;
	return S_OK;
}

///////////////////////////////////////////////

# include "Wrappers.h"

STDMETHODIMP CComCursor::LoadImageFromFile (BSTR _strFileName, long _x0, long _y0) {
	USES_CONVERSION;

	// Open image file.
	const CFileForRead hFile (OLE2A (_strFileName));
	if (! hFile.IsValid ())
		return E_INVALIDARG;

	// Read image file.
	const DWORD dwSize = ::GetFileSize (hFile, NULL);
	CGlobalPtr hGlobal (dwSize);
	void * const pFile = hGlobal.Lock ();
	DWORD dwRead = 0;
	::ReadFile (hFile, pFile, dwSize, & dwRead, NULL);
	if (dwRead < dwSize)
		return E_FAIL;

	// Make IStream for image file.
	CComPtr<IStream> pStream;
	HRESULT hr = ::CreateStreamOnHGlobal (hGlobal.Detach (), TRUE, & pStream);
	if (FAILED (hr))
		return hr;

	// Load from IStream.
	CComPtr<IPicture> pPicture;
	hr = ::OleLoadPicture (pStream, dwSize, FALSE, IID_IPicture, reinterpret_cast<void **> (& pPicture));
	if (FAILED (hr))
		return hr;

	m_cursor.pIcon  = pPicture;
	m_cursor.xIcon0 = _x0;
	m_cursor.yIcon0 = _y0;

	return S_OK;
}

STDMETHODIMP CComCursor::UnloadImage () {
	m_cursor.pIcon.Release ();
	return S_OK;
}

STDMETHODIMP CComCursor::get_Tag (VARIANT * _ret) {
	return ::VariantCopy (_ret, & m_cursor.vTag);
}

STDMETHODIMP CComCursor::put_Tag (const VARIANT * _pVal) {
	return m_cursor.vTag.Copy (_pVal);
}



